/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "DepthStencilState.h"

#include <D3D11.h>
#include <tinyxml.h>
#include "StringToEnum.h"

#include "../Managers/DX11Manager.h"

namespace Hogshead
{
	namespace DX11Rendering
	{
		ContentCPP(Hogshead::DX11Rendering::DepthStencilState, DepthStencilState)	

		DepthStencilState::DepthStencilState(void)
		{
			_depth_stencil_state = NULL;
		}


		DepthStencilState::~DepthStencilState(void)
		{
		}

		void DepthStencilState::finalize()
		{
			if(_depth_stencil_state)
				_depth_stencil_state->Release();
		}

		void DepthStencilState::explicitLoad()
		{
			throw Exception("You cannot load a DepthStencilState in this manor, XML is required");
		}

		void DepthStencilState::readXML(const String& in_xml)
		{
			//parse the String for what we need
			TiXmlDocument document;
			document.Parse(in_xml);

			TiXmlElement* root_element = document.RootElement();
			String root_tag(root_element->Value());

			if(root_tag.equals("Content"))
			{
				//get the standard stuff
				this->_key = String(root_element->Attribute("key"));
				this->_path = String(root_element->Attribute("path"));

				//used to get the absolute path to it 
				this->_path = ContentLibrary::getCompleteContentString( this->_path );

				//set up our variables with default values
				bool depth_enable = true;
				D3D11_DEPTH_WRITE_MASK depth_write_mask = D3D11_DEPTH_WRITE_MASK_ALL;
				D3D11_COMPARISON_FUNC depth_func = 	D3D11_COMPARISON_LESS; 
				bool stencil_enable = false;
				UINT8 stencil_read_mask = D3D11_DEFAULT_STENCIL_READ_MASK;
				UINT8 stencil_write_mask = D3D11_DEFAULT_STENCIL_WRITE_MASK;

				//set up our defaults for front op and back op
				D3D11_DEPTH_STENCILOP_DESC op_desc;
				op_desc.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
				op_desc.StencilFunc = D3D11_COMPARISON_ALWAYS;
				op_desc.StencilPassOp = D3D11_STENCIL_OP_KEEP;
				op_desc.StencilFailOp = D3D11_STENCIL_OP_KEEP;

				D3D11_DEPTH_STENCILOP_DESC front_face = op_desc;
				D3D11_DEPTH_STENCILOP_DESC back_face = op_desc;

				root_element->QueryBoolAttribute("DepthEnable", &depth_enable);
				
				const char* depth_write_mask_string = root_element->Attribute("DepthWriteMask");
				if(depth_write_mask_string)
				{
					 if(!StringToEnum::_depth_write_mask_dic->tryGetValue(depth_write_mask_string, depth_write_mask))
						 error(StringBuilder() << "Depth write mask invalid:\n" << this->_path);
				}

				const char* depth_func_string = root_element->Attribute("DepthFunc");
				if(depth_func_string)
				{
					if(!StringToEnum::_comparison_func_dic->tryGetValue(depth_write_mask_string, depth_func))
						 error(StringBuilder() << "Depth function invalid:\n" << this->_path);
				}

				root_element->QueryBoolAttribute("StencilEnable", &stencil_enable);

				int stencil_read_mask_int;
				int result = root_element->QueryIntAttribute("StencileReadMask", &stencil_read_mask_int);
				if(result == TIXML_SUCCESS) 
					stencil_read_mask = (UINT8)stencil_read_mask_int;

				int stencil_write_mask_int;
				result = root_element->QueryIntAttribute("StencileWriteMask", &stencil_write_mask_int);
				if(result == TIXML_SUCCESS) 
					stencil_write_mask = (UINT8)stencil_write_mask_int;

				//read in values for front face operations and functions
				const char* front_face_stencil_fail_op_string = root_element->Attribute("FrontFaceStencilFailOp");
				if(front_face_stencil_fail_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(front_face_stencil_fail_op_string, front_face.StencilFailOp))
					 {
						 error(StringBuilder() << "Invalid Front Face Stencil Fail Op:\n" << this->_path);
					 }
				}

				const char* front_face_stencil_depth_fail_op_string = root_element->Attribute("FrontFaceStencilDepthFailOp");
				if(front_face_stencil_depth_fail_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(front_face_stencil_depth_fail_op_string, front_face.StencilDepthFailOp))
					 {
						 error(StringBuilder() << "Invalid Front Face Stencil Depth Fail Op:\n" << this->_path);
					 }
				}

				const char* front_face_stencil_pass_op_string = root_element->Attribute("FrontFaceStencilPassOp");
				if(front_face_stencil_pass_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(front_face_stencil_pass_op_string, front_face.StencilPassOp))
					 {
						 error(StringBuilder() << "Invalid Front Face Stencil Pass Op:\n" << this->_path);
					 }
				}

				const char* front_face_stencil_func_string = root_element->Attribute("FrontFaceStencilFunc");
				if(front_face_stencil_func_string)
				{
					if(!StringToEnum::_comparison_func_dic->tryGetValue(front_face_stencil_func_string, front_face.StencilFunc))
					 {
						 error(StringBuilder() << "Invalid Front Face Stencil Func:\n" << this->_path);
					 }
				}

				//read in values for back face operations and functions
				const char* back_face_stencil_fail_op_string = root_element->Attribute("BackFaceStencilFailOp");
				if(back_face_stencil_fail_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(back_face_stencil_fail_op_string, back_face.StencilFailOp))
					 {
						 error(StringBuilder() << "Invalid Back Face Stencil Fail Op:\n" << this->_path);
					 }
				}

				const char* back_face_stencil_depth_fail_op_string = root_element->Attribute("BackFaceStencilDepthFailOp");
				if(back_face_stencil_depth_fail_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(back_face_stencil_depth_fail_op_string, back_face.StencilDepthFailOp))
					 {
						 error(StringBuilder() << "Invalid Back Face Stencil Depth Fail Op:\n" << this->_path);
					 }
				}

				const char* back_face_stencil_pass_op_string = root_element->Attribute("BackFaceStencilPassOp");
				if(back_face_stencil_pass_op_string)
				{
					if(!StringToEnum::_stencil_op_dic->tryGetValue(back_face_stencil_pass_op_string, back_face.StencilPassOp))
					 {
						 error(StringBuilder() << "Invalid Back Face Stencil Pass Op:\n" << this->_path);
					 }
				}

				const char* back_face_stencil_func_string = root_element->Attribute("BackFaceStencilFunc");
				if(back_face_stencil_func_string)
				{
					if(!StringToEnum::_comparison_func_dic->tryGetValue(back_face_stencil_func_string, back_face.StencilFunc))
					 {
						 error(StringBuilder() << "Invalid Back Face Stencil Func:\n" << this->_path);
					 }
				}

				D3D11_DEPTH_STENCIL_DESC desc;
				desc.BackFace = back_face;
				desc.DepthEnable = depth_enable;
				desc.DepthFunc = depth_func;
				desc.DepthWriteMask = depth_write_mask;
				desc.FrontFace = front_face;
				desc.StencilEnable = stencil_enable;
				desc.StencilReadMask = stencil_read_mask;
				desc.StencilWriteMask = stencil_write_mask;

				
				HRESULT hr = DX11Manager::createDepthStencilState(&desc, &this->_depth_stencil_state);
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON CONTENT LOAD: Depth stencil State could not be created:" << _path;
					DX11Manager::_logger->write(sb);
					throw new Exception(sb);
				}
#ifdef _DEBUG
				hr = _depth_stencil_state->SetPrivateData( WKPDID_D3DDebugObjectName, _path.length() - 1, _path );
				if(FAILED(hr))
				{
					StringBuilder sb;
					sb << "ERROR ON LOAD: Depth Stencil State could not be named: " << _path;
					DX11Manager::_logger->write(sb);
					throw new Exception(sb);
				}
#endif
			}

			return;
		}
	}
}
